#options dialog

import wx
import wx.lib.colourselect as csel

import resources

ID_COLOUR_CHOOSER = wx.NewId()
ID_UNDO_COLOUR = wx.NewId()
ID_SHOW_TRAY = wx.NewId()

ID_SHOW_CURSOR = wx.NewId()

pref = []

class FullScreenPanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.check_hide = wx.CheckBox(self, ID_SHOW_CURSOR, "Nascondi puntatore automaticamente (*)", (5, 10))
        self.check_hide.SetValue(resources.get_preferences("Schermo intero").get("hide cursor", False))
        self.Bind(wx.EVT_CLOSE, self.on_close)
    
    def on_close(self, event):
        self.save_pref()
    
    def save_pref(self):
        global pref
        pref.append(("Schermo intero", {"Hide Cursor": self.check_hide.IsChecked()}))


class InterfacePanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        sizer = wx.GridBagSizer(2, 2) 

        colour_label = wx.StaticText(self, -1, "Colore di sfondo attuale: ")
        self.colour_button = csel.ColourSelect(self, ID_COLOUR_CHOOSER)
        undo_colour_button = wx.Button(self, ID_UNDO_COLOUR, "Ripristina")
        
        self.icon_check = wx.CheckBox(self, ID_SHOW_TRAY, "Mostra icona nella tray bar (*)")
        
        sizer.Add(colour_label, (0, 0), flag=wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border=5)
        sizer.Add(self.colour_button, (0, 1), flag=wx.TOP|wx.BOTTOM|wx.RIGHT, border=5)
        sizer.Add(undo_colour_button, (0, 2), flag=wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_LEFT)
        sizer.Add(self.icon_check, (1, 0), (1, 2), wx.LEFT|wx.RIGHT, border=5)
        
        self.SetSizer(sizer)
        
        self.options = resources.get_preferences("Interfaccia")
        
        colour = self.options.get("colour", (0, 0, 0)) 
        show_tray = self.options.get("show tray", False)
        self.colour_button.SetValue(colour)
        self.icon_check.SetValue(show_tray)
        
        del colour, show_tray #useless variables, so we can delete them
        
        self.Bind(csel.EVT_COLOURSELECT, self.on_select_colour, id=ID_COLOUR_CHOOSER)
        self.Bind(wx.EVT_BUTTON, self.on_undo_colour, id=ID_UNDO_COLOUR)
        self.Bind(wx.EVT_CHECKBOX, self.on_show_tray, id=ID_SHOW_TRAY)
        self.Bind(wx.EVT_CLOSE, self.on_close)
    
    def on_close(self, event):
        self.save_pref()
    
    def save_pref(self):
        global pref
        col = self.colour_button.GetValue()
        show = self.icon_check.IsChecked()
        pref.append(("Interfaccia", {"colour": col[:3], "show tray": show}))
        del col, show
    
    def on_show_tray(self, event):
        self.GetParent().GetParent().options['show tray'] = self.icon_check.IsChecked()

    def on_undo_colour(self, event):
        #let's set back the colour to default (black)
        main = self.GetParent().GetParent().GetParent()#the main frame
        self.colour_button.SetValue((0, 0, 0))
        self.GetParent().GetParent().options['colour'] = (0, 0, 0)
        main.backcolour = (0, 0, 0)
        #main.refresh()
    
    def on_select_colour(self, event):
        main = self.GetParent().GetParent().GetParent() 
        self.GetParent().GetParent().options['colour'] = (event.GetValue()[:3])#the third element of the tuple is the alpha channel.
        main.backcolour = (event.GetValue()[:3])
        #main.refresh()

panels = { "Interfaccia": InterfacePanel, 
            "Schermo intero": FullScreenPanel } #other panels can be easily added
            
class NoSelPanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        wx.StaticText(self, -1, "Seleziona una categoria\n\nLe voci contrassegnate da * necessitano di riavvio.")

class OptionDialog(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, size=(430, 250))
        
        self.options = {}
        
        self.main_p = wx.Panel(self, -1)
        self.sizer = wx.FlexGridSizer(rows=2, cols=2)
        self.list = wx.ListBox(self.main_p, 0, style=wx.LB_SINGLE|wx.LB_NEEDED_SB)
        self.old_panel = NoSelPanel(self.main_p, -1)
        
        button_panel = wx.Panel(self.main_p, -1)
        ok_button = wx.Button(button_panel, wx.ID_OK, "Ok", pos=(0, 0))
        canc_button = wx.Button(button_panel, wx.ID_CANCEL, "Annulla", pos=(80, 0))
        
        self.list.Append("Interfaccia")
        self.list.Append("Schermo intero")

        self.sizer.Add(self.list, 1, wx.EXPAND|wx.ALL, border=5)
        self.sizer.Add(self.old_panel, 1, wx.EXPAND|wx.TOP|wx.BOTTOM|wx.RIGHT, border=5)
        self.sizer.AddSpacer(1)
        self.sizer.Add(button_panel, 1, wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT, border=5)
        self.sizer.AddGrowableRow(0, 1)
        self.sizer.AddGrowableCol(1, 1)
        
        self.main_p.SetSizer(self.sizer)
        
        self.Bind(wx.EVT_BUTTON, self.on_ok, id=wx.ID_OK)
        self.Bind(wx.EVT_BUTTON, self.on_canc, id=wx.ID_CANCEL)
        self.Bind(wx.EVT_LISTBOX, self.on_list_sel, id=0)

        self.Show()
    
    def on_list_sel(self, event):
        fs = panels[event.GetString()](self.main_p, -1)
        self.sizer.Replace(self.old_panel, fs)
        self.sizer.Layout()
        self.old_panel.Close()
        self.old_panel.Destroy()
        self.old_panel = fs
        del fs
    
    def on_ok(self, event):
        self.old_panel.save_pref()
        for t in pref:
            resources.write_preferences(t[0], t[1])
        self.GetParent().refresh()
        wx.CallAfter(self.Destroy)
    
    def on_canc(self, event):
        del self.options
        wx.CallAfter(self.Destroy)

if __name__ == "__main__":
    a = wx.PySimpleApp()
    b = OptionDialog(None, "Titolo")
    a.MainLoop()
